package dome2;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 括号匹配问题
 * User: 王圆豪
 * Date: 2024-10-16
 * Time: 19:29
 */
class SeqStack {
    private char[] stack; // 存储栈元素
    private int top; // 栈顶索引
    private int capacity; // 栈的容量

    // 构造函数
    public SeqStack(int capacity) {
        this.capacity = capacity;
        this.stack = new char[capacity];
        this.top = -1; // 初始化栈顶索引
    }

    // 入栈操作
    public void push(char item) {
        if (top == capacity - 1) {
            System.out.println("Stack is full");
            return;
        }
        stack[++top] = item; // 将元素压入栈中
    }

    // 出栈操作
    public char pop() {
        if (isEmpty()) {
            System.out.println("Stack is empty");
            return '\0'; // 返回空字符
        }
        return stack[top--]; // 返回栈顶元素并移除
    }

    // 检查栈是否为空
    public boolean isEmpty() {
        return top == -1;
    }
}
public class ParenthesisMatcher {
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入括号序列：");
        String input = scanner.nextLine();

        // 创建一个栈实例
        SeqStack stack = new SeqStack(input.length());

        boolean isMatched = true;

        for (char ch : input.toCharArray()) {
            if (ch == '(') {
                stack.push(ch); // 左括号入栈
            } else if (ch == ')') {
                if (stack.isEmpty()) {
                    isMatched = false; // 没有匹配的左括号
                    break;
                } else {
                    stack.pop(); // 右括号出栈
                }
            }
        }

        // 如果栈不为空，说明还有未匹配的左括号
        if (isMatched && stack.isEmpty()) {
            System.out.println("Match");
        } else {
            System.out.println("NoMatch");
        }

        scanner.close();
    }
}
